#-*- coding: utf-8 -*-
'''
Created on 2.14, 2016
@author: XFBY
'''
import requests as re
import json
def BGetMethod():
    url = 'https://kyfw.12306.cn/otn/lcxxcx/query?purpose_codes=ADULT&queryDate=2016-02-19&from_station=NCG&to_station=CZQ'
    url2= 'https://kyfw.12306.cn/otn/lcxxcx/query?purpose_codes=ADULT&queryDate=2016-02-19&from_station=NCG&to_station=CZQ'
    headers = { 
                    "Accept":"text/html,application/xhtml+xml,application/xml;",
                    "Accept-Encoding":"gzip",
                    "Accept-Language":"zh-CN,zh;q=0.8",
                    "User-Agent":"Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/42.0.2311.90 Safari/537.36"
                  }
    ts = re.Session()
    ts.verify = False #关闭https验证   
    ts.headers = headers
    r = ts.get(url)
    result = json.loads(r.text)['data']['datas']
    print(result)
    print(r.text)
    pass
def GetMethod():
    url = 'http://www.baidu.com'
    payload = {'key1':'value1','key2':'value2'}
    r = re.get(url, payload)
    print(r.text)
    
def PostMethod():
    url1 = 'http://www.exanple.com/login'
    url2 = 'http://www.example.com/main'
    data = {"user":"user","password":"pass"}
    headers = {"Accept":"text/html,application/xhtml+xml,application/xml;",
            "Accept-Encoding":"gzip",
            "Accept-Language":"zh-CN,zh;q=0.8",
            "Referer":"http://www.example.com/",
            "User-Agent":"Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/42.0.2311.90 Safari/537.36"
            }
    res1 = re.post(url1, data=data, headers=headers)
    res2 = re.get(url2, cookies=res1.cookies, headers=headers)
    print (res2.content)#获得二进制响应内容
    print (res2.raw)#获得原始响应内容,需要stream=True
    print (res2.raw.read(50))
    print (type(res2.text))#返回解码成unicode的内容
    print (res2.url)
    print (res2.history)#追踪重定向
    print (res2.cookies)
    print (res2.cookies['example_cookie_name'])
    print (res2.headers)
    print (res2.headers['Content-Type'])
    print (res2.headers.get('content-type'))
    print (res2.json)#讲返回内容编码为json
    print (res2.encoding)#返回内容编码
    print (res2.status_code)#返回http状态码
    print (res2.raise_for_status())#返回错误状态码
    pass
#PostMethod()
#GetMethod()
print('********************')
BGetMethod()
print(123456)
